/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.peelable;

import com.funcom.peeler.component.CanvasWindow;
import com.funcom.peeler.symbol.Symbol;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilderFactory;

public class PeelCanvasWindow extends PeelContainer {

    public PeelCanvasWindow() {
        this( new CanvasWindow() );
    }

    protected PeelCanvasWindow( CanvasWindow canvas_window ) {
        super( canvas_window );
        element_name = Symbol.Node.canvas_window;
    }

    public Document peel() {

        Document doc = null;

        try {
            doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        }
        catch ( Exception e ) {
            System.out.println( e );
            return null;
        }

        Element root = doc.createElement( Symbol.Node.root );
        doc.appendChild( root );

        if ( getRoot().getStyleSheetPath() != null ) {
            root.setAttribute( Symbol.Attr.stylesheet, getRoot().getStyleSheetPath() );
        }

        for ( PeelComponent peelable : children ) {

            if ( peelable != null ) {
                Element peeled_component = peelable.peel( doc );
                root.appendChild( peeled_component );
            }
        }

        return doc;
    }

    public CanvasWindow getComponent() {
        return (CanvasWindow)component;
    }
}
